package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.business.domain.FileInternalTransferInPerson;
import com.ruoyi.business.domain.bo.FileInternalTransferInPersonBo;
import com.ruoyi.business.domain.vo.FileInternalTransferInPersonVo;
import com.ruoyi.business.mapper.FileInternalTransferInPersonMapper;
import com.ruoyi.business.service.IFileInternalTransferInPersonService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 内部转入人员中间Service业务层处理
 *
 * @author sal
 * @date 2024-12-04
 */
@RequiredArgsConstructor
@Service
public class FileInternalTransferInPersonServiceImpl implements IFileInternalTransferInPersonService {

    private final FileInternalTransferInPersonMapper baseMapper;

    /**
     * 新增内部转入人员中间
     */
    @Override
    public Boolean insertByBo(FileInternalTransferInPersonBo bo) {
        FileInternalTransferInPerson add = BeanUtil.toBean(bo, FileInternalTransferInPerson.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    private MPJLambdaWrapper<FileInternalTransferInPerson> buildQueryWrapper(FileInternalTransferInPersonBo bo) {
        MPJLambdaWrapper<FileInternalTransferInPerson> lqw = new MPJLambdaWrapper<>();
        lqw.selectAll(FileInternalTransferInPerson.class)
            .eq(bo.getInId() != null, FileInternalTransferInPerson::getInId, bo.getInId())
            .eq(bo.getPersonId() != null, FileInternalTransferInPerson::getPersonId, bo.getPersonId())
            .groupBy(FileInternalTransferInPerson::getId);
        return lqw;
    }

    @Override
    public List<Long> queryByInId(FileInternalTransferInPersonBo bo) {
        MPJLambdaWrapper<FileInternalTransferInPerson> joinQueryWrapper = buildQueryWrapper(bo);
        List<FileInternalTransferInPersonVo> voList = baseMapper.selectJoinList(FileInternalTransferInPersonVo.class, joinQueryWrapper);
        ArrayList<Long> personIDs = new ArrayList<>();
        for (FileInternalTransferInPersonVo fileInternalTransferInPersonVo : voList) {
            personIDs.add(fileInternalTransferInPersonVo.getPersonId());
        }
        return personIDs;
    }

    @Override
    public Boolean deleteByPersonId(Long personId) {
        baseMapper.delete(new LambdaQueryWrapper<FileInternalTransferInPerson>().eq(FileInternalTransferInPerson::getPersonId, personId));
        return null;
    }

    @Override
    public Boolean deleteByInId(Long inId) {
        baseMapper.delete(new LambdaQueryWrapper<FileInternalTransferInPerson>().eq(FileInternalTransferInPerson::getInId, inId));
        return null;
    }
}
